package com.kws.commercial.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.commercial.dao.CommercialProjectQuotationManagement;
import com.kws.commercial.dao.Info.QueryProjectQuotationInfo;
import com.kws.commercial.dao.Rq.CreateProjectQuotationRq;
import com.kws.commercial.dao.Rq.CreateQuotationDetailsRq;
import com.kws.commercial.dao.Rq.CreateSingleQuotationRq;
import com.kws.commercial.mapper.CommercialProjectQuotationMapper;
import com.kws.commercial.service.CommercialProjectQuotationService;
import com.kws.commercial.service.CommercialSchemeQuotationService;
import com.kws.commercial.service.CommercialSingleQuotationService;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <b>CommercialProjectQuotationServiceImpl</b>
 *
 * @description: CommercialProjectQuotationServiceImpl <br>
 * @date: 2024/10/28 09:26 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class CommercialProjectQuotationServiceImpl extends ServiceImpl<CommercialProjectQuotationMapper, CommercialProjectQuotationManagement> implements CommercialProjectQuotationService {
    private static final Logger logger = LoggerFactory.getLogger(CommercialProjectQuotationServiceImpl.class);

    @Autowired
    private CommercialSchemeQuotationService commercialSchemeQuotationService;

    @Override
    public PageInfo<QueryProjectQuotationInfo> queryProjectQuotationPage(Integer pageNum, Integer pageSize, String schemeName) {
        PageHelper.startPage(pageNum,pageSize);
        Long companyId = SecurityUtils.getCompanyId();
        List<QueryProjectQuotationInfo> projectQuotationList = baseMapper.queryProjectQuotationPage(schemeName,companyId);
        PageInfo<QueryProjectQuotationInfo> pageInfo = new PageInfo<>(projectQuotationList);
        return pageInfo;
    }

    @Override
    public boolean createQuotationDetails(CreateQuotationDetailsRq rq) {
        return commercialSchemeQuotationService.createQuotationDetails(rq);
    }

    @Override
    public boolean createProjectQuotation(CreateProjectQuotationRq rq) {
        if (CollectionUtil.isNotEmpty(rq.getCompanyIdList())){
            List<CommercialProjectQuotationManagement> list = new ArrayList<>();
            rq.getCompanyIdList().forEach(item -> {
                CommercialProjectQuotationManagement commercialProjectQuotationManagement = new CommercialProjectQuotationManagement();
                commercialProjectQuotationManagement.setUserId(SecurityUtils.getUserId());
                commercialProjectQuotationManagement.setCompanyId(item);
                commercialProjectQuotationManagement.setSchemeName(rq.getSchemeName());
                commercialProjectQuotationManagement.setSchemeStatus("0");
                commercialProjectQuotationManagement.setOptionUrl(rq.getOptionUrl());
                //计算报价有效期
                Date date = new Date();
                LocalDate localDate = LocalDate.parse(DateUtil.format(new Date(),DatePattern.NORM_DATE_FORMATTER));
                LocalDate enddDate = LocalDate.parse(rq.getDeadlineDate());
                long between = ChronoUnit.DAYS.between(localDate, enddDate);
                commercialProjectQuotationManagement.setEffectiveTime(Long.bitCount(between));
                Ksys.setBasicModelData(commercialProjectQuotationManagement,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
                list.add(commercialProjectQuotationManagement);
            });

            boolean b = this.saveBatch(list);
            return b;
        }
        return false;
    }

    @Override
    public QueryProjectQuotationInfo queryProjectQuotationById(Long id) {
        return baseMapper.queryProjectQuotationById(id);
    }

    @Override
    public BigDecimal createSingleQuotation(CreateSingleQuotationRq rq) {
        return null;
    }

    @Override
    public List<CommercialProjectQuotationManagement> querySchemeList() {
        Long companyId = SecurityUtils.getCompanyId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.eq("enable","1");
        List<CommercialProjectQuotationManagement> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
